package com.nccsjz.back.game.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.nccsjz.back.game.service.GameService;
import com.nccsjz.base.BaseAction;
import com.nccsjz.pojo.Game;
import com.nccsjz.utils.Pager;
@SuppressWarnings("serial")
public class GameAction  extends BaseAction{
	//存放url中的参数
	private long id;
	//存放分页信息
    private Pager pager;
    //页码
    private int pageNo=1;
    //每页多少行
    private int pageSize=20;
    //存放多选中的id
    private String[] checkbox;
    private File[] myFile;
    private String[] myFilecontentType;
    private String[] myFileFileName;
    private String gameTitle;
    private String savePath;
    private Game game;
    private GameService gameService;
    private String gameDesc;
    
	public String getGameTitle() {
		return gameTitle;
	}
	public void setGameTitle(String gameTitle) {
		this.gameTitle = gameTitle;
	}
	public String getGameDesc() {
		return gameDesc;
	}
	public void setGameDesc(String gameDesc) {
		this.gameDesc = gameDesc;
	}
	public File[] getMyFile() {
		return myFile;
	}
	public void setMyFile(File[] myFile) {
		this.myFile = myFile;
	}
	public String[] getMyFilecontentType() {
		return myFilecontentType;
	}
	public void setMyFilecontentType(String[] myFilecontentType) {
		this.myFilecontentType = myFilecontentType;
	}
	public String[] getMyFileFileName() {
		return myFileFileName;
	}
	public void setMyFileFileName(String[] myFileFileName) {
		this.myFileFileName = myFileFileName;
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	   private String getSavePath() throws Exception 
		{
	        return ServletActionContext.getRequest().getRealPath(savePath);
	    }
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public Pager getPager() {
		return pager;
	}
	public void setPager(Pager pager) {
		this.pager = pager;
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public String[] getCheckbox() {
		return checkbox;
	}
	public void setCheckbox(String[] checkbox) {
		this.checkbox = checkbox;
	}
	public Game getGame() {
		return game;
	}
	public void setGame(Game game) {
		this.game = game;
	}
    //pager dispaly game
	public String list_Game() throws Exception{
		gameService=new GameService();
		pager=gameService.getGame(pageSize, pageNo);
		return SUCCESS;
	}
    //首页显示
	public String listF_Game() throws Exception{
		gameService=new GameService();
		pager=gameService.getGame(pageSize, pageNo);
		return "listf";
	}
	
	//delete one game
	public String delete_Game() throws Exception{
	    gameService=new GameService();
	    gameService.deleteGameFile(id);//删除游戏文件
	    boolean flag=gameService.deleteGame(id);//删除数据库记录
	    
	    if(flag){
		return "list";
		}else{
	     return ERROR;
		}
	}
	//view one game
	public String view_Game() throws Exception{
		gameService=new GameService();
	    game=gameService.getGame(id);    
		return "view";
	}	
	
	//page display game by name
	public String list_GameByTitle() throws Exception{
		gameService=new GameService();
		pager=gameService.getGame(pageSize, pageNo, gameTitle);
		return SUCCESS;
	}
	public String deleteBatch_Game() throws SQLException{
		gameService=new GameService();
		List list=new ArrayList();
		for(int i=0;i<checkbox.length;i++){
			list.add(Long.parseLong(checkbox[i]));
		}
		gameService.deldetBatchGame(list);
		return "list";
	}
	//add game
    public String add_Game() throws Exception{
    	File[] files = getMyFile();
		FileOutputStream fos=null;
		FileInputStream fis=null;
		//检查上传目录是否存在
		utils.FileUtils.makeDir(getSavePath());
		String gamePath=getSavePath()+File.separator+getMyFileFileName()[0];
		String gamePic=getSavePath()+File.separator+getMyFileFileName()[1];
		for (int i = 0 ; i < files.length ; i++)
		{
			//以服务器的文件保存地址和原文件名建立上传文件输出流
			fos = new FileOutputStream(getSavePath() + File.separator +getMyFileFileName()[i]);
			fis = new FileInputStream(files[i]);
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0)
			{
				fos.write(buffer , 0 , len);
			}
		}
         fos.close();// 注意：流应当关闭。
         fis.close();
         Game game=new Game();
         game.setGametitle(gameTitle);
         game.setGamepath(gamePath);
         game.setUploaddate(new Date());
         game.setGamedesc(gameDesc);
         game.setGamepic(gamePic);
         gameService=new GameService();
         boolean flag=gameService.addGame(game);
    	 if(flag){
         return "list";
         }else{
        	 return ERROR;
         }
    }
}
